from typing import Any, Dict
from course.helpers import group_course_by_category
from django.shortcuts import render
from django.views.generic import DetailView
from django.views import View
from .models import Course, Introduction


class CourseListView(View):
    def get(self, request):
        courses = Course.objects.all()
        course_dict, views_dict = group_course_by_category(courses)
        return render(
            request,
            'course/list.html',
            {
                'course_dict': course_dict,
                'views_dict': views_dict,
                'user': request.user
            }
        )


class CourseDetailView(DetailView):

    queryset = Course.objects.all()
    pk_url_kwarg = 'course_id'
    template_name = 'course/detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # Get introduction infos for each course.
        context['introduction'] = Introduction.objects.filter(
            course_id=kwargs.get('object').id).first()

        return context
